package ch.il07.converter.presentation.beans;

import java.util.ArrayList;
import java.util.List;

import ch.il07.converter.business.MeasurandAction;
import ch.il07.converter.business.ServiceFactoryAdapter;
import ch.il07.converter.persistence.entities.Measurand;
import ch.il07.converter.presentation.Helper;
import ch.il07.converter.presentation.beans.entities.MeasurandBean;

public class ControllerMeasurandsBean {
	private List<MeasurandBean> measurands = null;
	
	public ControllerMeasurandsBean() {
		loadAllMeasurands();
	}
	
	public void loadAllMeasurands() {
		measurands = new ArrayList<MeasurandBean>();
		MeasurandAction ma = ServiceFactoryAdapter.getMeasurandActionInstance();
		List<Measurand> privateMeasurands = ma.getAllPrivateMeasurands(Helper.getUserName());
		
//		XXX Currently administrative operations are not implemented
//		if(Helper.isAdministrator()) {
//			if(privateMeasurands == null) {
//				privateMeasurands = new ArrayList<Measurand>();
//			}
//			for(Measurand m : ma.getAllPublicMeasurands()) {
//				privateMeasurands.add(m);
//			}
//		}
		
		displayMeasurands(privateMeasurands);
	}
	
	public String process() {
		return "stay";
	}
		
	public String vote() {
		// XXX Missing functionality on business layer (Measurand and Unit class), *Issue 17*
		return "stay";
	}
	
	public String delete() {
		MeasurandAction ma = ServiceFactoryAdapter.getMeasurandActionInstance();
		for(MeasurandBean m : getSelectedMeasurands()) {
			ma.deletePrivateMeasurand(Helper.getUserName(), m.getName());
		}
		// Reload GUI
		// XXX Fix, because changes on the GUI (but not DB) are made even if checkboxes are not selected.
		loadAllMeasurands();
		return "stay"; 
	}
	
	public String save() {
		MeasurandAction ma = ServiceFactoryAdapter.getMeasurandActionInstance();
		for(MeasurandBean m : getSelectedMeasurands()) {
			// XXX Changing owner and name does not work atm.
			ma.editMeasurand(m.getOwner(), m.getOwner(), m.getOldName(), m.getName(), m.getDescription());
		}
		// Reload GUI
		// XXX Fix, because changes on the GUI (but not DB) are made even if checkboxes are not selected.
		loadAllMeasurands();
		
		return "stay";
	}
	
	private void displayMeasurands(List<Measurand> privateMeasurands) {
		for(Measurand m : privateMeasurands) {
			String owner = m.getOwner() != null ? m.getOwner().getUsername() : null;
			MeasurandBean mb = new MeasurandBean(
				owner,
				m.getName(),
				m.getDefaultUnit().getName(),
				m.getDescription()
			);
			measurands.add(mb);
		}
	}

	private List<MeasurandBean> getSelectedMeasurands() {
		List<MeasurandBean> selected = new ArrayList<MeasurandBean>();
		for(MeasurandBean mb : measurands) {
			if(mb.isSelected()) {
				selected.add(mb);
			}
		}
		return selected;
	}

	/**
	 * @return the measurands
	 */
	public List<MeasurandBean> getMeasurands() {
		return measurands;
	}

	/**
	 * @param measurands the measurands to set
	 */
	public void setMeasurands(List<MeasurandBean> measurands) {
		this.measurands = measurands;
	}

}